<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body>
    <!-- form结构 -->
    <form action="http://ajax-api.itheima.net/api/data" method="post">
      <input type="text" name="username" placeholder="请输入用户名" /><br />
      <input type="password" name="password" placeholder="请输入密码" /><br />
      <button type="submit">登录</button>
    </form>

    <!-- 引入axios -->
    <script src="./js/axios.js"></script>
    <script>
      //1.获取元素
      const form = document.querySelector('form')

      //2.绑定提交事件
      form.addEventListener('submit', function (e) {
        //阻止默认提交行为
        e.preventDefault()
        //收集用户数据
        const username = document
          .querySelector('input[name=username]')
          .value.trim()
        const password = document
          .querySelector('input[name=password]')
          .value.trim()

        //发起ajax请求
        axios({
          url: 'http://ajax-api.itheima.net/api/login',
          method: 'POST',
          //post请求写data参数
          data: {
            username,
            password,
          },
          //get请求写params参数
          // params: {},
        })
          //收集响应结果
          .then(({ data: res }) => {
            console.log(res)
          })

        // axios.get(url, { params: { 具体参数 } })

        // axios.post(url,{具体参数})
      })
    </script>
  </body>
</html>
